package com.plant.service.impl;

import com.plant.dao.ClassifyDao;
import com.plant.entity.Classify;
import com.plant.entity.GoodsOrder;
import com.plant.entity.Parameter;
import com.plant.service.ClassifyService;
import jakarta.annotation.Resource;
import lombok.extern.log4j.Log4j;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

import static com.plant.config.Detail.DETAIL_PREFIX;

/**
 * (Classify)表服务实现类
 *
 * @author makejava
 * @since 2024-08-25 19:00:35
 */
@Slf4j
@Service("classifyService")
public class ClassifyServiceImpl implements ClassifyService {
    @Resource
    RedisTemplate redisTemplate;
    @Resource
    private ClassifyDao classifyDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Classify queryById(Integer id) {
        return this.classifyDao.queryById(id);
    }

    @Override
    public List<Classify> classList() {
        return classifyDao.classList();
    }

    @Override
    public List<Parameter> parameterList(Integer id) {
        return classifyDao.parameterList(id);
    }

    @Override
    public Boolean cartAdd(Parameter parameter) {
        //判断前端值是否传递成功
        if (parameter == null) {
            return false;
        }
        //组成我所需要的key
        String key = DETAIL_PREFIX + parameter.getUid()+parameter.getOrderId();

        try {
            Object value = redisTemplate.opsForValue().get(key);
            // 打印获取到的原始值，便于调试
            System.out.println("Retrieved value from Redis: " + value);

            if (value != null) {
                Parameter parameter1 = (Parameter) value;

                if (parameter.getOrderId().equals(parameter1.getOrderId()) ) {
                    // 更新值
                    // 更新值
                    parameter1.setCount(parameter1.getCount() + 1);

                    // 删除键
                    redisTemplate.delete(key);

                    // 将值更新后存入Redis
                    redisTemplate.opsForValue().set(key, parameter1);

                    return true;
                }


            }
        } catch (Exception e) {
            // 异常处理
            e.printStackTrace();
            return false;
        }
        parameter.setCount(1);

        // 存储新的参数
        redisTemplate.opsForValue().set(key, parameter);

        return true;
    }


    @Override
    public List<Parameter> cartList(Integer uid) {

        return redisTemplate.opsForValue().multiGet(redisTemplate.keys("Detail"+uid+"*"));
    }

    @Override
    public Boolean orderAdd(GoodsOrder goodsOrder) {
        GoodsOrder goodsOrder1 = new GoodsOrder();

        goodsOrder1.setOrderTime(new Date());
        goodsOrder1.setStatus(0);
        goodsOrder1.setUid(goodsOrder.getUid());
        goodsOrder1.setOrderId(goodsOrder.getOrderId());
        goodsOrder1.setPid(goodsOrder.getPid());
        goodsOrder1.setGoodsMoney(goodsOrder.getGoodsMoney());
        goodsOrder1.setGoodsName(goodsOrder.getGoodsName());
        if (goodsOrder.getOrderIdList()!=null) {
            String[] split = goodsOrder.getOrderIdList().split(",");
            if (split.length != 0){
                for (String s : split) {
                    redisTemplate.delete("Detail"+goodsOrder.getUid() + s);
                }
            }
        }
        classifyDao.orderAdd(goodsOrder1);


        return true;

    }

    @Override
    public Boolean UpStatus(String orderId) {
        return classifyDao.UpStatus(orderId);

    }

    @Override
    public Boolean deRedis(Long orderId) {
        System.out.println(orderId);
        List<Parameter> parameters = redisTemplate.opsForValue().multiGet(redisTemplate.keys("Detail*"));
        if (parameters != null && parameters.size() > 0) {
            redisTemplate.delete("Detail" + orderId);
            return true;
        }
        return false;


    }

    @Override
    public Parameter orderList(String orderId) {

        // 使用 selectList() 方法获取所有记录
        Parameter resultList = classifyDao.orderList(orderId);


        return resultList;
    }

    @Override
    public Parameter orderList1(String orderId) {
        // 使用 selectList() 方法获取所有记录
        return classifyDao.orderList1(orderId);
    }

    @Override
    public Boolean orderInstrel(GoodsOrder goodsOrder) {
        return null;
    }


}
